/**
 * @author Linrui
 * @since 2011-05-06
 * 产品，控制层类
 */
package com.qeado.product.web;

import java.io.File;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.qeado.product.model.Product;
import com.qeado.product.model.ProductDTO;
import com.qeado.product.service.ProductService;
import com.qeado.sort.model.Sort;
import com.qeado.sort.service.SortService;
import com.qeado.user.model.User;
import com.qeado.util.FileUpload;
import com.qeado.util.PageHelper;

@Scope("session")
@Controller
public class ProductAction extends ActionSupport {

	@Autowired
	private ProductService productService;
	@Autowired
	private SortService sortService;

	private List<Sort> sortList;
	private List<Sort> unitlist;
	private List<Sort> brandList;
	private PageHelper productList;
	private ProductDTO productDTO;
	private Product product;
	private String pageSize = "10";
	private String pageNum = "1";
	
    private File myFile; // 上传的文件
    private String contentType; // 上传的文件类型
    private String fileName; // 上传文件的文件名
   
    public void setMyFileContentType(String contentType) {
        this.contentType = contentType;
   } 
   
    public void setMyFileFileName(String fileName) {
        this.fileName = fileName;
   } 
       
    public void setMyFile(File myFile) {
        this.myFile = myFile;
   } 

	public String getPageNum() {
		return pageNum;
	}

	public void setPageNum(String pageNum) {
		this.pageNum = pageNum;
	}

	private String orderBy = "create_date";
	private String asc = "desc";
	private String queryType = "1";
	private String urlMark = "1"; // 用于标记次链接是一个发布操作还是更新操作 1：发布 2：修改

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public String getUrlMark() {
		return urlMark;
	}

	public void setUrlMark(String urlMark) {
		this.urlMark = urlMark;
	}

	public List<Sort> getSortList() {
		return sortList;
	}

	public void setSortList(List<Sort> sortList) {
		this.sortList = sortList;
	}

	public List<Sort> getUnitlist() {
		return unitlist;
	}

	public void setUnitlist(List<Sort> unitlist) {
		this.unitlist = unitlist;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getAsc() {
		return asc;
	}

	public void setAsc(String asc) {
		this.asc = asc;
	}

	public String getQueryType() {
		return queryType;
	}

	public void setQueryType(String queryType) {
		this.queryType = queryType;
	}

	public List<Sort> getBrandList() {
		return brandList;
	}

	public void setBrandList(List<Sort> brandList) {
		this.brandList = brandList;
	}

	public PageHelper getProductList() {
		return productList;
	}

	public void setProductList(PageHelper productList) {
		this.productList = productList;
	}

	public ProductDTO getProductDTO() {
		return productDTO;
	}

	public void setProductDTO(ProductDTO productDTO) {
		this.productDTO = productDTO;
	}

	@Override
	public String execute() throws Exception {
		if (brandList == null)
			brandList = sortService.listByPcode("BR");
		if (unitlist == null)
			unitlist = sortService.listByPcode("UN");
		if (sortList == null)
			sortList = sortService.listAllProdSort();
		if (product != null)
			product = null;
		return super.execute();
	}

	public String listProductBySort() throws Exception {

		return null;
	}

	public String publicProduct() throws Exception {
		User user = (User) ActionContext.getContext().getSession().get(
				"login_user");
		FileUpload upload = null;
		if( myFile != null ){
			upload = new FileUpload();
			upload.setBasePath( ServletActionContext.getServletContext().getRealPath( "uploadfiles" ) );
			upload.setUserDir(user.getId());
			upload.setMyFile(myFile);
			upload.setFileName(fileName);
		}
		productDTO.setCreator(user.getUserName());
		productDTO.setProdOwner(user.getUserName());
		productDTO.setProdPlace();
		queryType = productDTO.getInfoCategory();
		if (productService.insertProduct(productDTO, upload)) {
			pageNum = "1"; // 跳转到第一页
			return productListByUser();
			// return null;//"public_success_1";
		} else {
			return "visit_fail";
		}

	}

	public String productListByUser() throws Exception {
		User user = (User) ActionContext.getContext().getSession().get(
				"login_user");
		if (user != null) {
			productList = productService.listByUser(Integer
					.parseInt(pageNum), Integer
					.parseInt(pageSize), user.getUserName(), orderBy, asc,
					queryType);
			productList.setPagebar("${ctx}/my/listByUser?pageSize="
					+ pageSize + "&orderBy=" + orderBy + "&asc=" + asc
					+ "&queryType=" + queryType);
			return "list";
		} else
			return "fail";
	}

	public String updateProduct() throws Exception {
		// productDTO.setCreator(product.getCreator());
		// productDTO.setProdOwner(product.getProdOwner());
		if (product == null)
			return "visit_fail";
		productDTO.setId(product.getId());
		productDTO.setProdPlace();
		queryType = productDTO.getInfoCategory();
		if (productService.updateProduct(productDTO)) {
			return productListByUser();
			// return null;//"public_success_1";
		} else {
			return "visit_fail";
		}
	}

	public String getProductById() throws Exception {
		if (brandList == null)
			brandList = sortService.listByPcode("BR");
		if (unitlist == null)
			unitlist = sortService.listByPcode("UN");
		if (sortList == null)
			sortList = sortService.listAllProdSort();
		product = productService.getProductById(productDTO.getId());
		urlMark = "update";
		if (product != null)
			return "view";
		else
			return "visit_fail";
	}

	public String deleteProductById() throws Exception {
		if (productService.deleteProductById(productDTO.getId()))
			return productListByUser();
		else
			return "visit_fail";
	}

}
